/*
 * Created on 15.01.2004
 * Local Projects Database
 * Mihai Postelnicu (mihai@ro-gateway.org)
 * (c) 2003 by eRomania Gateway
 */
package ro.gateway.aida.struts.action;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import ro.gateway.aida.db.DBWrapper;
import ro.gateway.aida.obj.AIDASector;
import ro.gateway.aida.obj.ActivityStatus;
import ro.gateway.aida.obj.Country;
import ro.gateway.aida.obj.Organization;
import ro.gateway.aida.obj.Person;
import ro.gateway.aida.obj.db.AIDASectorPersMan;
import ro.gateway.aida.obj.db.ActivityStatusDB;
import ro.gateway.aida.obj.db.OrganizationDB;
import ro.gateway.aida.obj.db.PersonDB;
import ro.gateway.aida.struts.MultiTilesAction;
import ro.gateway.aida.struts.form.EditProjectFilterForm;
import ro.gateway.aida.usr.User;
import ro.gateway.aida.usr.UserUtils;
import ro.gateway.aida.usr.messaging.Alerter;
import ro.gateway.aida.usr.messaging.ProjectFilter;
import ro.gateway.aida.utils.PageToken;
import ro.gateway.aida.utils.Utils;
import ro.xblue.translator.TranslatorUtils;
/**
 * @author Mihai Postelnicu
 *         <p>
 * 
 * e-mail (mihai@ro-gateway.org) <br>
 * (c) 2003 by eRomania Gateway
 * <p>
 */
public class ProjectFilterAction extends MultiTilesAction {
	private DBWrapper db;
	protected Alerter getCurrentAlerter(HttpServletRequest request) {
		HttpSession session = request.getSession();
		return (Alerter) session.getAttribute("CurrentAlerter");
	}
	public ActionForward modePrepare(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		User user = UserUtils.getLogged(request, response);
		EditProjectFilterForm eaf = (EditProjectFilterForm) form;
		if (user == null)
			return mapping.findForward("plslogin");
		HttpSession session = request.getSession();
		db = DBWrapper.getManager(token);
		if (session.getAttribute(Country.LIST) == null)
			session.setAttribute(Country.LIST, db.get(new Country(null)));
		String extern_modifier = request.getParameter("extern_modifier");
		if (session.getAttribute("CurrentAlerter") == null) {
			String alerter_id = request.getParameter("alerter_id");
			Alerter m = (Alerter) db.get(new Alerter(
					Long.parseLong(alerter_id), -1));
			session.setAttribute("CurrentAlerter", m);
		}
		if ("donor".equals(extern_modifier)) {
			List l = (List) session.getAttribute("SelectedOrgs");
			if ((l != null) && (l.size() != 0))
				eaf.setDonorOrg(((Organization) l.get(0)).getId());
			else
				eaf.setDonorOrg(-1);
		}
		if ("beneficiary".equals(extern_modifier)) {
			List l = (List) session.getAttribute("SelectedOrgs");
			if ((l != null) && (l.size() != 0))
				eaf.setBeneficiaryOrg(((Organization) l.get(0)).getId());
			else
				eaf.setBeneficiaryOrg(-1);
		}
		if ("funding".equals(extern_modifier)) {
			List l = (List) session.getAttribute("SelectedOrgs");
			if ((l != null) && (l.size() != 0))
				eaf.setFundingOrg(((Organization) l.get(0)).getId());
			else
				eaf.setFundingOrg(-1);
		}
		if ("contracting".equals(extern_modifier)) {
			List l = (List) session.getAttribute("SelectedOrgs");
			if ((l != null) && (l.size() != 0))
				eaf.setContractingOrg(((Organization) l.get(0)).getId());
			else
				eaf.setContractingOrg(-1);
		}
		if ("sector".equals(extern_modifier)) {
			AIDASector s = AIDASectorPersMan.getManager(token).getById(
					Integer.parseInt(request.getParameter("sector_id")));
			if (s != null)
				eaf.setSector(s.getRo_aida_code());
			else
				eaf.setSector(-1);
		}
		if ("person".equals(extern_modifier)) {
			Person p = (Person) session.getAttribute(Person.SELECTED);
			if (p != null)
				eaf.setInvolvedPerson(p.getId());
			else
				eaf.setInvolvedPerson(-1);
		}
		if (session.getAttribute(ActivityStatus.LIST) == null)
			session.setAttribute(ActivityStatus.LIST, ActivityStatusDB
					.getManager(token).getHarmonized());
		grabObjects(mapping, form, request, response);
		return modeSelect(mapping, form, request, response);
	}
	public void grabObjects(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String lang = TranslatorUtils.getCurrentLanguage(request,
				getServlet().getServletContext()).getLanguage();
		EditProjectFilterForm eaf = (EditProjectFilterForm) form;
		if (eaf.getSector() != -1) {
			AIDASector as = AIDASectorPersMan.getManager(token).getById(
					eaf.getSector());
			as.setViewLang(lang);
			request.setAttribute("Sector", as);
		}
		if (eaf.getContractingOrg() != -1) {
			Organization o = OrganizationDB.getManager(token).getByID(
					eaf.getContractingOrg());
			o.setViewLang(lang);
			request.setAttribute("ContractingOrg", o);
		}
		if (eaf.getBeneficiaryOrg() != -1) {
			Organization o = OrganizationDB.getManager(token).getByID(
					eaf.getBeneficiaryOrg());
			o.setViewLang(lang);
			request.setAttribute("BeneficiaryOrg", o);
		}
		if (eaf.getFundingOrg() != -1) {
			Organization o = OrganizationDB.getManager(token).getByID(
					eaf.getFundingOrg());
			o.setViewLang(lang);
			request.setAttribute("FundingOrg", o);
		}
		if (eaf.getDonorOrg() != -1) {
			Organization o = OrganizationDB.getManager(token).getByID(
					eaf.getDonorOrg());
			o.setViewLang(lang);
			request.setAttribute("DonorOrg", o);
		}
		if (eaf.getInvolvedPerson() != -1) {
			Person p = PersonDB.getManager(token).getByID(
					eaf.getInvolvedPerson());
			request.setAttribute("Person", p);
		}
	}
	public ActionForward modeView(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		User user = UserUtils.getLogged(request, response);
		long id = Long.parseLong(request.getParameter("id"));
		ProjectFilter m = (ProjectFilter) db.get(new ProjectFilter(id, -1));
		if (m == null)
			return mapping.findForward("notfound");
		request.setAttribute("filter", m);
		Utils.setBreadCrumbs(request, "bc.prjfilter.view");
		return mapping.findForward("view");
	}
	public ActionForward modeList(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		HttpSession session = request.getSession();
		List sl = (List) session.getAttribute(LIST);
		if (sl == null)
			return modeRefresh(mapping, form, request, response);
		else {
			PageToken.tokenizeList(request, sc, sl, "filters");
			Utils.setBreadCrumbs(request, "bc.prjfilter.list");
			return mapping.findForward("list");
		}
	}
	public ActionForward modeRefresh(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		User user = UserUtils.getLogged(request, response);
		HttpSession session = request.getSession();
		session.setAttribute(LIST, db.get(new ProjectFilter(-1,
				getCurrentAlerter(request).getId())));
		List sl = (List) session.getAttribute(LIST);
		PageToken.tokenizeList(request, sc, sl, "filters");
		Utils.setBreadCrumbs(request, "bc.prjfilter.list");
		return mapping.findForward("list");
	}
	public ActionForward modeEdit(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		User user = UserUtils.getLogged(request, response);
		long id = Long.parseLong(request.getParameter("id"));
		EditProjectFilterForm eaf = (EditProjectFilterForm) form;
		ProjectFilter m = (ProjectFilter) db.get(new ProjectFilter(id, -1));
		eaf.setAlerterId(m.getAlerterId());
		eaf.setBeneficiaryOrg(m.getBeneficiaryOrg());
		eaf.setContractingOrg(m.getContractingOrg());
		eaf.setCountry_iso3(m.getCountry_iso3());
		eaf.setDescription(m.getDescription());
		eaf.setTitle(m.getTitle());
		eaf.setDonorOrg(m.getDonorOrg());
		eaf.setFundingOrg(m.getFundingOrg());
		eaf.setKeywords(m.getKeywords());
		eaf.setId(m.getId());
		eaf.setInvolvedPerson(m.getInvolvedPerson());
		eaf.setSector(m.getSector());
		eaf.setStat(m.getStat());
		grabObjects(mapping, form, request, response);
		Utils.setBreadCrumbs(request, "bc.prjfilter.edit");
		return mapping.getInputForward();
	}
	public ActionForward modeSave(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		User user = UserUtils.getLogged(request, response);
		EditProjectFilterForm eaf = (EditProjectFilterForm) form;
		ProjectFilter m = new ProjectFilter(eaf.getId(), eaf.getAlerterId());
		m.setAlerterId(eaf.getAlerterId());
		m.setBeneficiaryOrg(eaf.getBeneficiaryOrg());
		m.setContractingOrg(eaf.getContractingOrg());
		m.setCountry_iso3(eaf.getCountry_iso3());
		m.setDescription(eaf.getDescription());
		m.setTitle(eaf.getTitle());
		m.setDonorOrg(eaf.getDonorOrg());
		m.setFundingOrg(eaf.getFundingOrg());
		m.setId(eaf.getId());
		m.setInvolvedPerson(eaf.getInvolvedPerson());
		m.setStat(eaf.getStat());
		m.setKeywords(eaf.getKeywords());
		m.setSector(eaf.getSector());
		db.synchronize(m);
		return modeRefresh(mapping, form, request, response);
	}
	public ActionForward modeNew(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		EditProjectFilterForm eaf = (EditProjectFilterForm) form;
		eaf.setAlerterId(getCurrentAlerter(request).getId());
		return mapping.getInputForward();
	}
	public ActionForward modeDelete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		User user = UserUtils.getLogged(request, response);
		String[] delfilters = request.getParameterValues("delfilters");
		if (delfilters != null)
			for (int i = 0; i < delfilters.length; i++)
				db.delete(new ProjectFilter(Long.parseLong(delfilters[i]), -1));
		return modeRefresh(mapping, form, request, response);
	}
	public ActionForward modeSelect(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String mode = request.getParameter("mode");
		String delete = request.getParameter("delete");
		if ("view".equals(mode))
			return modeView(mapping, form, request, response);
		if ("refresh".equals(mode))
			return modeRefresh(mapping, form, request, response);
		if ("list".equals(mode))
			return modeList(mapping, form, request, response);
		if ("edit".equals(mode))
			return modeEdit(mapping, form, request, response);
		if ("save".equals(mode))
			return modeSave(mapping, form, request, response);
		if (delete != null)
			return modeDelete(mapping, form, request, response);
		return modeNew(mapping, form, request, response);
	}
	public static final String LIST = "ProjectFilter_List";
}